package com.wsoft.hr.pm.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.core.exception.Asserts;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.hr.pm.entity.HrPmScopeEntity;
import com.wsoft.hr.pm.manager.HrPmScopeManager;
import com.wsoft.hr.pm.mapper.HrPmScopeMapper;
import com.wsoft.hr.pm.query.scope.ScopeInsertQuery;
import com.wsoft.hr.pm.query.scope.ScopeListQuery;
import com.wsoft.hr.pm.query.scope.ScopeUpdateQuery;
import com.wsoft.hr.pm.service.IHrPmScopeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 维度表 服务实现类
 * </p>
 *
 * @author Z001
 * @since 2025年02月24日 11:18:27
 */
@Service
public class HrPmScopeServiceImpl extends ServiceImpl<HrPmScopeMapper, HrPmScopeEntity> implements IHrPmScopeService {

    @Resource
    private HrPmScopeManager scopeManager;

    /**
     * 新增维度
     * @param query
     * @return
     */
    @Override
    public boolean insertScope(ScopeInsertQuery query) {
        checkScopeName(query.getName(),null);
        HrPmScopeEntity entity = Convert.convert(HrPmScopeEntity.class, query);
        entity.setHrOrgId(LoginUserUtil.getUserInfo().getHrOrgId());
        return scopeManager.save(entity);
    }

    /**
     * 修改维度
     * @param query
     * @return
     */
    @Override
    public boolean updateScope(ScopeUpdateQuery query) {
        //校验
        checkScopeName(query.getName(),query.getId());
        HrPmScopeEntity entity = this.getById(query.getId());
        checkOrgId(entity.getHrOrgId());
        HrPmScopeEntity convert = Convert.convert(HrPmScopeEntity.class, query);
        return scopeManager.updateById(convert);
    }

    /**
     * 获取维度详情
     * @param id
     * @return
     */
    @Override
    public HrPmScopeEntity getScopeInfo(Long id) {
        return scopeManager.getById(id);
    }

    /**
     * 删除维度
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        return scopeManager.removeById(id);
    }

    /**
     * 维度分页
     * @param query
     * @return
     */
    @Override
    public PageInfo<HrPmScopeEntity> listScope(ScopeListQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<HrPmScopeEntity> list = scopeManager.list(query);
        return PageInfo.of(list);
    }

    // ################################## 私有方法  ##################################

    /**
     * 判断orgId 是否为当前登录用的orgId
     * @param orgId
     */
    private void checkOrgId(Long orgId){
        Long curOrgId = LoginUserUtil.getUserInfo().getHrOrgId();
        if(null == orgId || !orgId.equals(curOrgId)){
            //非当前组织分组 不能删除
            Asserts.fail("[hr.pm.group.orgId.error]");
        }
    }

    /**
     * 校验分组名称
     * @param name
     * @param id
     */
    private void checkScopeName(String name,Long id){
        HrPmScopeEntity orgAndName = scopeManager.getOrgAndName(LoginUserUtil.getUserInfo().getHrOrgId(), name);
        if(null == id && null != orgAndName){
            Asserts.fail("[hr.pm.name.error]");
        }
        if(null != id && null != orgAndName && !id.equals(orgAndName.getId())){
            Asserts.fail("[hr.pm.name.error]");
        }
    }
}
